Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Pure function</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Pure_function"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Pure_function rootpage-Pure_function skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Pure function</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1305433154">
/* start https://en.wikipedia.org/ */


.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}


/* end https://en.wikipedia.org/ */
</style>
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>pure function</b> is a <a href="Subroutine" class="mw-redirect" title="Subroutine">function</a> that has the following properties:<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<ol><li>the function <a href="Return_statement" title="Return statement">return values</a> are <a href="Relational_operator#Location_equality_vs._content_equality" title="Relational operator">identical</a> for identical <a href="Argument_of_a_function" title="Argument of a function">arguments</a> (no variation with local <a href="Static_variable" title="Static variable">static variables</a>, <a href="Non-local_variable" title="Non-local variable">non-local variables</a>, mutable <a href="Value_type_and_reference_type" title="Value type and reference type">reference arguments</a> or <a href="Input/output" title="Input/output">input streams</a>, i.e., <a href="Referential_transparency" title="Referential transparency">referential transparency</a>), and</li>
<li>the function has no <a href="Side_effect_(computer_science)" title="Side effect (computer science)">side effects</a> (no mutation of non-local variables, mutable reference arguments or input/output streams).</li></ol>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Pure_functions">Pure functions</h3></div>
<p>The following examples of <a href="C%2B%2B" title="C++">C++</a> functions are pure:
</p>
<div><ul><li><code>floor</code>, returning the <a href="Floor_and_ceiling_functions" title="Floor and ceiling functions">floor</a> of a number;</li><li><code>max</code>, returning the <a href="Maxima_and_minima" class="mw-redirect" title="Maxima and minima">maximum</a> of two values.</li><li>the function <i>f</i>, defined as
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">atomic</span><span class="o">&lt;</span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="o">++</span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
The value of <code>x</code> can be only observed inside other invocations of <code>f()</code>, and as <code>f()</code> does not communicate the value of <code>x</code> to its environment, it is indistinguishable from function <code>void f() {}</code> that does nothing. Note that <code>x</code> is <code>std::atomic</code> so that modifications from multiple threads executing <code>f()</code> concurrently do not result in a <a href="Race_condition#Data_race" title="Race condition">data race</a>, which has <a href="Undefined_behavior" title="Undefined behavior">undefined behavior</a> in C and C++.</li></ul></div>
<div class="mw-heading mw-heading3"><h3 id="Impure_functions">Impure functions</h3></div>
<p>The following C++ functions are impure as they lack the above property 1:
</p>
<div><ul><li>because of return value variation with a static variable
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="o">++</span><span class="n">x</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li><li>because of return value variation with a non-local variable
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
For the same reason, e.g. the C++ library function <code>sin()</code> is not pure, since its result depends on the <a href="IEEE_rounding_mode" class="mw-redirect" title="IEEE rounding mode">IEEE rounding mode</a> which can be changed at runtime.</li><li>because of return value variation with a mutable reference argument
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="kt">int</span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li><li>because of return value variation with an input stream
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cin</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li></ul></div>
<p>The following C++ functions are impure as they lack the above property 2:
</p>
<div><ul><li>because of mutation of a local static variable
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="o">++</span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li><li>because of mutation of a non-local variable
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">++</span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li><li>because of mutation of a mutable reference argument
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">f</span><span class="p">(</span><span class="kt">int</span><span class="o">*</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="o">++*</span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li><li>because of mutation of an output stream
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">void</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">"Hello, world!"</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li></ul></div>
<p>The following C++ functions are impure as they lack both the above properties 1 and 2:
</p>
<div><ul><li>because of return value variation with a local static variable and mutation of a local static variable
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="o">++</span><span class="n">x</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li><li>because of return value variation with an input stream and mutation of an input stream
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">f</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cin</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></li></ul></div>
<div class="mw-heading mw-heading2"><h2 id="I/O_in_pure_functions">I/O in pure functions</h2></div>
<p>I/O is inherently impure: input operations undermine <a href="Referential_transparency" title="Referential transparency">referential transparency</a>, and output operations create side effects. Nevertheless, there is a sense in which a function can perform input or output and still be pure, if the sequence of operations on the relevant I/O devices is modeled explicitly as both an argument and a result, and I/O operations are taken to fail when the input sequence does not describe the operations actually taken since the program began execution.
</p><p>The second point ensures that the only sequence usable as an argument must change with each I/O action; the first allows different calls to an I/O-performing function to return different results on account of the sequence arguments having changed.<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p><p>The <a href="I/O_monad" class="mw-redirect" title="I/O monad">I/O monad</a> is a <a href="Programming_idiom" title="Programming idiom">programming idiom</a> typically used to perform I/O in pure functional languages.
</p>
<div class="mw-heading mw-heading2"><h2 id="Memoization">Memoization</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Memoization" title="Memoization">Memoization</a></div>
<p>The outputs of a pure function can be <a href="Cache_(computing)" title="Cache (computing)">cached</a> in a look-up table. Any result that is returned from a given function is cached, and the next time the function is called with the same input parameters, the cached result is returned instead of computing the function again.
</p><p><a href="Memoization" title="Memoization">Memoization</a> can be performed by wrapping the function in another function (<a href="Wrapper_function" title="Wrapper function">wrapper function</a>).<sup id="cite_ref-Aley_2017_p._109_5-0" class="reference"><a href="#cite_note-Aley_2017_p._109-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup>
</p><p>By means of memoization, the computational effort involved in the computations of the function itself can be reduced, at the cost of the overhead for managing the cache and an increase of memory requirements.
</p><p>A <a href="C_(programming_language)" title="C (programming language)">C</a> program for cached computation of <a href="Factorial" title="Factorial">factorial</a> (<a href="Assert.h" title="Assert.h"><code>assert()</code></a> aborts with an error message if its argument is false; on a 32-bit machine, values beyond <code>fact(12)</code> cannot be represented.)
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">fact</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="n">fact</span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span>
<span class="p">}</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">fact_wrapper</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">cache</span><span class="p">[</span><span class="mi">13</span><span class="p">];</span>
<span class="w"> </span><span class="n">assert</span><span class="p">(</span><span class="mi">0</span><span class="w"> </span><span class="o">&lt;=</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">13</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">cache</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="n">cache</span><span class="p">[</span><span class="n">n</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fact</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">cache</span><span class="p">[</span><span class="n">n</span><span class="p">];</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Compiler_optimizations">Compiler optimizations</h2></div>
<p>Functions that have just the above property 2 – that is, have no side effects – allow for compiler optimization techniques such as <a href="Common_subexpression_elimination" title="Common subexpression elimination">common subexpression elimination</a> and <a href="Loop_optimization" title="Loop optimization">loop optimization</a> similar to arithmetic operators.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> A C++ example is the <code>length</code> method, returning the size of a string, which depends on the memory contents where the string points to, therefore lacking the above property 1. Nevertheless, in a <a href="Thread_(computing)" title="Thread (computing)">single-threaded environment</a>, the following C++ code
</p>
<div class="mw-highlight mw-highlight-lang-c++ mw-content-ltr" dir="ltr"><pre><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"Hello, world!"</span><span class="p">;</span>
<span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">};</span>
<span class="kt">int</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>

<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">10</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">length</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];</span>
<span class="p">}</span>
</pre></div>
<p>can be optimized such that the value of <code>s.length()</code> is computed only once, before the loop.
</p><p>Some programming languages allow for declaring a pure property to a function:
</p>
<ul><li>In <a href="Fortran" title="Fortran">Fortran</a> and D, the <code>pure</code> keyword can be used to declare a function to be just side-effect free (i.e. have just the above property 2).<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> The compiler may be able to deduce property 1 on top of the declaration.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> See also: <a href="Fortran_95_language_features#Pure_procedures" title="Fortran 95 language features">Fortran 95 language features §&nbsp;Pure procedures</a>.</li>
<li>In the <a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GCC</a>, the <code>pure</code> attribute specifies property 2, while the <code>const</code> attribute specifies a truly pure function with both properties.<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup></li>
<li>Languages offering <a href="Compile-time_function_execution" title="Compile-time function execution">compile-time function execution</a> may require functions to be pure, sometimes with the addition of some other constraints. Examples include <code>constexpr</code> of C++ (both properties).<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> See also: <a href="C%2B%2B11#constexpr_–_Generalized_constant_expressions" title="C++11">C++11 §&nbsp;constexpr – Generalized constant expressions</a>.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="Unit_testing">Unit testing</h2></div>
<p>Since pure functions have identical <a href="Return_statement" title="Return statement">return values</a> for identical <a href="Argument_of_a_function" title="Argument of a function">arguments</a>, they are well suited to <a href="Unit_testing" title="Unit testing">unit testing</a>.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Compile-time_function_execution" title="Compile-time function execution">Compile-time function execution</a>&nbsp;– The evaluation of pure functions at compile time</li>
<li><a href="Deterministic_algorithm" title="Deterministic algorithm">Deterministic algorithm</a>&nbsp;– Algorithm that, given a particular input, will always produce the same output</li>
<li><a href="Idempotence" title="Idempotence">Idempotence</a>&nbsp;– Property of operations whereby they can be applied multiple times without changing the result</li>
<li><a href="Lambda_calculus" title="Lambda calculus">Lambda calculus</a>&nbsp;– Mathematical-logic system based on functions</li>
<li><a href="Purely_functional_data_structure" title="Purely functional data structure">Purely functional data structure</a>&nbsp;– Data structure implementable in purely functional languages</li>
<li><a href="Reentrancy_(computing)" title="Reentrancy (computing)">Reentrancy (computing)</a>&nbsp;– Executing a function concurrently without interfering with other invocations</li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFBartosz_Milewski2013" class="citation web cs1">Bartosz Milewski (2013). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20161027145455/https://www.schoolofhaskell.com/school/starting-with-haskell/basics-of-haskell/3-pure-functions-laziness-io">"Basics of Haskell"</a>. <i>School of Haskell</i>. FP Complete. Archived from <a rel="nofollow" class="external text" href="https://www.schoolofhaskell.com/school/starting-with-haskell/basics-of-haskell/3-pure-functions-laziness-io">the original</a> on 2016-10-27<span class="reference-accessdate">. Retrieved <span class="nowrap">2018-07-13</span></span>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFBrian_Lonsdorf2015" class="citation web cs1">Brian Lonsdorf (2015). <a rel="nofollow" class="external text" href="https://github.com/MostlyAdequate/mostly-adequate-guide/blob/master/ch03.md">"Professor Frisby's Mostly Adequate Guide to Functional Programming"</a>. <i>GitHub</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-03-20</span></span>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite id="haskell98" class="citation book cs1">Peyton Jones, Simon L. (2003). <a rel="nofollow" class="external text" href="http://it.bmc.uu.se/andlov/dev/books/haskell98-report.pdf"><i>Haskell 98 Language and Libraries: The Revised Report</i></a> <span class="cs1-format">(PDF)</span>. Cambridge, United Kingdom: Cambridge University Press. p.&nbsp;95. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>0-521 826144</bdi><span class="reference-accessdate">. Retrieved <span class="nowrap">17 July</span> 2014</span>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="curry14" class="citation web cs1">Hanus, Michael. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20140725231927/http://www.informatik.uni-kiel.de/~curry/papers/report.pdf">"Curry: An Integrated Functional Logic Language"</a> <span class="cs1-format">(PDF)</span>. <i>www-ps.informatik.uni-kiel.de</i>. Institut für Informatik, Christian-Albrechts-Universität zu Kiel. p.&nbsp;33. Archived from <a rel="nofollow" class="external text" href="http://www.informatik.uni-kiel.de/~curry/papers/report.pdf">the original</a> <span class="cs1-format">(PDF)</span> on 25 July 2014<span class="reference-accessdate">. Retrieved <span class="nowrap">17 July</span> 2014</span>.</cite></span>
</li>
<li id="cite_note-Aley_2017_p._109-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-Aley_2017_p._109_5-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFAley2017" class="citation book cs1">Aley, R. (2017). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=_JY3DwAAQBAJ&amp;pg=PA109"><i>Pro Functional PHP Programming: Application Development Strategies for Performance Optimization, Concurrency, Testability, and Code Brevity</i></a>. SpringerLink&nbsp;: Bücher. Apress. p.&nbsp;109. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-1-4842-2958-3</bdi><span class="reference-accessdate">. Retrieved <span class="nowrap">2024-02-04</span></span>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gcc-6.1.0/gcc/Common-Function-Attributes.html#index-functions-that-have-no-side-effects-3246">"Common Function Attributes - Using the GNU Compiler Collection (GCC)"</a>. <i>gcc.gnu.org, the GNU Compiler Collection</i>. Free Software Foundation, Inc<span class="reference-accessdate">. Retrieved <span class="nowrap">2018-06-28</span></span>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://archive.today/20130103042647/http://publib.boulder.ibm.com/infocenter/comphelp/v111v131/index.jsp?topic=/com.ibm.xlf131.aix.doc/language_ref/pure.html">Pure attribute in Fortran</a></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20190202194918/https://digitalmars.com/d/2.0/function.html#pure-functions">Pure attribute in D language</a></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html">"Common Function Attributes"</a>. <i>Using the GNU Compiler Collection (GCC</i><span class="reference-accessdate">. Retrieved <span class="nowrap">22 July</span> 2021</span>.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20190325075719/https://en.cppreference.com/w/cpp/language/constexpr">constexpr attribute in C++</a></span>
</li>
</ol></div></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-05-21" href="https://en.wikipedia.org/wiki/?title=Pure_function&amp;oldid=1291437073">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>